package com.jsm.twentyfour.controller;

import com.jsm.twentyfour.constant.RoomAccountStatus;
import com.jsm.twentyfour.domain.Account;
import com.jsm.twentyfour.domain.RoomAccount;
import com.jsm.twentyfour.dto.AccountDTO;
import com.jsm.twentyfour.dto.RoomDTO;
import com.jsm.twentyfour.reponse.BaseResponse;
import com.jsm.twentyfour.reponse.PageResponse;
import com.jsm.twentyfour.request.*;
import com.jsm.twentyfour.service.BaseService;
import com.jsm.twentyfour.service.RoomAccountService;
import com.jsm.twentyfour.service.RoomService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * create by jsm on 2019/1/23
 */
@RestController
public class RoomController extends BaseController {
    @Resource
    private RoomService roomService;
    @Resource
    private RoomAccountService roomAccountService;

    @PostMapping("/room")
    public BaseResponse <RoomDTO> post(@RequestBody CreateRoomRequest request) {
        return new BaseResponse <>(roomService.createRoom(request.getUserId()));
    }

    @PostMapping("/room/join")
    public BaseResponse <RoomDTO> join(@RequestBody JoinRoomRequest request) {
        return new BaseResponse <>(roomService.joinRoom(request.getUserId(), request.getRoomId()));
    }

    @PostMapping("/room/leave")
    public BaseResponse <Void> leave(@RequestBody LeaveRoomRequest request) {
        roomService.leaveRoom(request.getUserId(), request.getRoomId());
        return new BaseResponse <>();
    }

    @GetMapping("/room/list")
    public PageResponse <RoomDTO> list(ListRoomRequest request) {
        BaseService.PageList <RoomDTO> list = roomService.list(request);
        PageResponse <RoomDTO> pageResponse = new PageResponse <>();
        pageResponse.setPageNo(request.getPageNo());
        pageResponse.setPageSize(request.getPageSize());
        pageResponse.setTotal(list.getTotal());
        pageResponse.setData(list.getList());
        return pageResponse;
    }

    @GetMapping("/room/players/list")
    public BaseResponse <List <AccountDTO>> playerList(ListRoomAccountRequest request) {
        List <AccountDTO> list = roomAccountService.playerList(request);
        return new BaseResponse <>(list);
    }

}
